/*
 *  Copyright (c) 2018 str2num. All Rights Reserved.
 *  Copyright (c) 2011, The WebRTC project authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree.
 */
  
/**
 * @file function_view.h
 * @author str2num
 * @brief 
 *  
 **/


#ifndef  __RTCBASE_FUNCTION_VIEW_H_
#define  __RTCBASE_FUNCTION_VIEW_H_

#include <type_traits>
#include <utility>

// Just like std::function, FunctionView will wrap any callable and hide its
// actual type, exposing only its signature. But unlike std::function,
// FunctionView doesn't own its callable---it just points to it. Thus, it's a
// good choice mainly as a function argument when the callable argument will
// not be called again once the function has returned.
//
// Its constructors are implicit, so that callers won't have to convert lambdas
// and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
// safe because FunctionView is only a reference to the real callable.
//
// Example use:
//
//   void SomeFunction(rtc::FunctionView<int(int)> index_transform);
//   ...
//   SomeFunction([](int i) { return 2 * i + 1; });
//
// Note: FunctionView is tiny (essentially just two pointers) and trivially
// copyable, so it's probably cheaper to pass it by value than by const
// reference.

namespace rtcbase {

template <typename T>
class FunctionView;  // Undefined.

template <typename RetT, typename... ArgT>
class FunctionView<RetT(ArgT...)> final {
public:
    // Constructor for lambdas and other callables; it accepts every type of
    // argument except those noted in its enable_if call.
    template <
        typename F,
        typename std::enable_if<
            // Not for function pointers; we have another constructor for that
            // below.
            !std::is_function<typename std::remove_pointer<
            typename std::remove_reference<F>::type>::type>::value &&
                // Not for nullptr; we have another constructor for that below.
                !std::is_same<std::nullptr_t,
            typename std::remove_cv<F>::type>::value &&
                // Not for FunctionView objects; we have another constructor for that
                // (the implicitly declared copy constructor).
                     !std::is_same<FunctionView,
            typename std::remove_cv<typename std::remove_reference<
                F>::type>::type>::value>::type* = nullptr>
                FunctionView(F&& f)
                : _call(call_void_ptr<typename std::remove_reference<F>::type>) {
                    _f.void_ptr = &f;
                }

    // Constructor that accepts function pointers. If the argument is null, the
    // result is an empty FunctionView.
    template <
        typename F,
        typename std::enable_if<std::is_function<typename std::remove_pointer<
        typename std::remove_reference<F>::type>::type>::value>::type* =
            nullptr>
        FunctionView(F&& f)
        : _call(f ? call_fun_ptr<typename std::remove_pointer<F>::type> : nullptr) 
        {
            _f.fun_ptr = reinterpret_cast<void (*)()>(f);
        }

    // Constructor that accepts nullptr. It creates an empty FunctionView.
    template <typename F,
             typename std::enable_if<std::is_same<
                 std::nullptr_t,
             typename std::remove_cv<F>::type>::value>::type* = nullptr>
                 FunctionView(F&& ) : _call(nullptr)  {}

    // Default constructor. Creates an empty FunctionView.
    FunctionView() : _call(nullptr) {}

    RetT operator()(ArgT... args) const {
        return _call(_f, std::forward<ArgT>(args)...);
    }

    // Returns true if we have a function, false if we don't (i.e., we're null).
    explicit operator bool() const { return !!_call; }

private:
    union VoidUnion {
        void* void_ptr;
        void (*fun_ptr)();
    };

    template <typename F>
    static RetT call_void_ptr(VoidUnion vu, ArgT... args) {
        return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
    }

    template <typename F>
    static RetT call_fun_ptr(VoidUnion vu, ArgT... args) {
        return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
                std::forward<ArgT>(args)...);
    }

    // A pointer to the callable thing, with type information erased. It's a
    // union because we have to use separate types depending on if the callable
    // thing is a function pointer or something else.
    VoidUnion _f;

    // Pointer to a dispatch function that knows the type of the callable thing
    // that's stored in f_, and how to call it. A FunctionView object is empty
    // (null) iff _call is null.
    RetT (*_call)(VoidUnion, ArgT...);
};

}  // namespace rtcbase

#endif  //__RTCBASE_FUNCTION_VIEW_H_


